package com.whz.workmanageol.service.impl;

import com.whz.workmanageol.dao.AdminMapper;
import com.whz.workmanageol.dao.StudentMapper;
import com.whz.workmanageol.dao.TeacherMapper;
import com.whz.workmanageol.entity.Admin;
import com.whz.workmanageol.entity.Student;
import com.whz.workmanageol.entity.Teacher;
import com.whz.workmanageol.entity.User;
import com.whz.workmanageol.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional
public class LoginServiceImpl implements LoginService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public User login(String userid, String password) {
        User user = new User();
        if(userid.startsWith("admin")){
            Example example = new Example(Admin.class);
            example.createCriteria().andEqualTo("adminid", userid);
            List<Admin> one = adminMapper.selectByExample(example);
            if(one.size() > 0){
                // 用户存在 校验密码
                Admin theOne = one.get(0);
                String thepassword = theOne.getPassword();
                if(password.equals(thepassword)){
                    // 用户身份校验通过 身份是admin
                    user.setId(theOne.getId());
                    user.setUserid(theOne.getAdminid());
                    user.setUsername(theOne.getUsername());
                    user.setTel(theOne.getTel());
                    user.setEmail(theOne.getEmail());
                    user.setRole("admin");
                }else {
                    // 用户密码错误
                    user.setPassword("");
                }
            } else {
                // 用户不存在，移除用户名
                user.setUserid("");
            }
        }
        else if(userid.length() == 6){
            // 身份可能是教师
            Example example = new Example(Teacher.class);
            example.createCriteria().andEqualTo("teacherid", userid);
            List<Teacher> teachers = teacherMapper.selectByExample(example);
            if(teachers.size() > 0){
                // 用户名存在
                Teacher theTeacher = teachers.get(0);
                String thepassword = theTeacher.getPassword();
                if(password.equals(thepassword)){
                    // 用户身份为教师
                    user.setId(theTeacher.getId());
                    user.setUserid(theTeacher.getTeacherid());
                    user.setUsername(theTeacher.getUsername());
                    user.setPassword(password);
                    user.setTel(theTeacher.getTel());
                    user.setEmail(theTeacher.getEmail());
                    user.setRole("teacher");
                }else {
                    // 密码错误
                    user.setPassword("");
                }

            }else {
                // 用户名不存在
                user.setUserid("");
            }
        }
        else if(userid.length() == 9){
            // 身份可能是学生
            Example example = new Example(Student.class);
            example.createCriteria().andEqualTo("studentid", userid);
            List<Student> students = studentMapper.selectByExample(example);
            if(students.size() > 0){
                // 用户名存在
                Student theStudent = students.get(0);
                if(password.equals(theStudent.getPassword())){
                    // 身份是学生
                    user.setId(theStudent.getId());
                    user.setUserid(theStudent.getStudentid());
                    user.setUsername(theStudent.getUsername());
                    user.setPassword(password);
                    user.setEmail(theStudent.getEmail());
                    user.setTel(theStudent.getTel());
                    user.setRole("student");
                }else {
                    // 密码不正确
                    user.setPassword("");
                }
            }else {
                // 用户名不存在
                user.setUserid("");
            }
        }
        return user;
    }

    @Override
    public User findByUserid(String userid) {
        User user = new User();
        if(userid.startsWith("admin")){
            Example example = new Example(Admin.class);
            example.createCriteria().andEqualTo("adminid", userid);
            List<Admin> one = adminMapper.selectByExample(example);
            if(one.size() > 0){
                // 用户存在 校验密码
                Admin theOne = one.get(0);
                String thepassword = theOne.getPassword();
                // 用户身份校验通过 身份是admin
                user.setId(theOne.getId());
                user.setUserid(theOne.getAdminid());
                user.setUsername(theOne.getUsername());
                user.setPassword(thepassword);
                user.setTel(theOne.getTel());
                user.setEmail(theOne.getEmail());
                user.setRole("admin");
            } else {
                // 用户不存在，移除用户名
                user.setUserid("");
            }
        }
        else if(userid.length() == 6){
            // 身份可能是教师
            Example example = new Example(Teacher.class);
            example.createCriteria().andEqualTo("teacherid", userid);
            List<Teacher> teachers = teacherMapper.selectByExample(example);
            if(teachers.size() > 0){
                // 用户名存在
                Teacher theTeacher = teachers.get(0);
                String thepassword = theTeacher.getPassword();
                // 用户身份为教师
                user.setId(theTeacher.getId());
                user.setUserid(theTeacher.getTeacherid());
                user.setUsername(theTeacher.getUsername());
                user.setPassword(thepassword);
                user.setTel(theTeacher.getTel());
                user.setEmail(theTeacher.getEmail());
                user.setRole("teacher");
            }else {
                // 用户名不存在
                user.setUserid("");
            }
        }
        else if(userid.length() == 9){
            // 身份可能是学生
            Example example = new Example(Student.class);
            example.createCriteria().andEqualTo("studentid", userid);
            List<Student> students = studentMapper.selectByExample(example);
            if(students.size() > 0){
                // 用户名存在
                Student theStudent = students.get(0);
                // 身份是学生
                user.setId(theStudent.getId());
                user.setUserid(theStudent.getStudentid());
                user.setUsername(theStudent.getUsername());
                user.setPassword(theStudent.getPassword());
                user.setEmail(theStudent.getEmail());
                user.setTel(theStudent.getTel());
                user.setRole("student");
            }else {
                // 用户名不存在
                user.setUserid("");
            }
        }
        return user;
    }
}
